package br.com.ctm.advogadope.mobile.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@FacesConverter(value = "PJeDataHoraConverter")
public class PJeDataHoraConverter implements Converter {
	
	private static final SimpleDateFormat pjeDateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yy HH:mm");

	public PJeDataHoraConverter() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public Object getAsObject(FacesContext context, UIComponent component, String value) {
		Date date = null;
		try {
			date = pjeDateFormat.parse(value);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component, Object object) {
		if (object instanceof String) {
			try {
				object = pjeDateFormat.parse((String)object);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		return dateFormat.format(object);
	}

}
